<template>
  <el-form
    ref="ruleFormRef"
    :model="userReg"
    status-icon
    :rules="rules"
    label-width="120px"
    class="demo-ruleForm"
  >
  <el-form-item label="用户名" prop="username">
      <el-input v-model.number="userReg.username" />
    </el-form-item>

    <el-form-item label="Password" prop="password">
      <el-input v-model="userReg.password" type="password" autocomplete="off" />
    </el-form-item>

    <el-form-item label="Confirm" prop="checkPass">
      <el-input
        v-model="userReg.checkPass"
        type="password"
        autocomplete="off"
      />

    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="submitForm(ruleFormRef)"
        >Submit</el-button
      >
      <el-button @click="resetForm(ruleFormRef)">Reset</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { register } from '@/api/User';
import type { FormInstance } from 'element-plus'
import  { ElMessage } from 'element-plus'
import { useCounterStore } from '@/stores/counter';
import type { UserAttr } from '@/modle/User';
import { useRouter } from 'vue-router';

const router = useRouter()
const store = useCounterStore()

const ruleFormRef = ref<FormInstance>()

const checkName = (rule: any, value: any, callback: any) => {
  if (!value) {
    return callback(new Error('请输入 the age'))
  }
  callback()
}

const validatePass = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入 the password'))
  } else {
    if (userReg.checkPass !== '') {
      if (!ruleFormRef.value) return
      ruleFormRef.value.validateField('checkPass', () => null)
    }
    callback()
  }
}
const validatePass2 = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入 the password again'))
  } else if (value !== userReg.password) {
    callback(new Error("Two inputs don't match!"))
  } else {
    callback()
  }
}

const userReg = reactive({
  password: '',
  checkPass: '',
  username: '',
})

const rules = reactive({
  password: [{ validator: validatePass, trigger: 'blur' }],
  checkPass: [{ validator: validatePass2, trigger: 'blur' }],
  username: [{ validator: checkName, trigger: 'blur' }],
})

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      register({
        userName:userReg.username,
        password:userReg.password
      }).then(res => {
        if(res.data.code === 200) {
          ElMessage({
    message: '注册成功',
    type: 'success',
      })
      store.setUser({
        userName:userReg.username,
        money:0
      })
      router.push('/')
      
        }else {
          ElMessage({
    message: '用户名已经存在',
    type: 'error',
      })
        }
    })
    } else {
      ElMessage({
    message: '不对',
    type: 'error',
      })
      return false
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>